package com.zttek.photosShareProject.modules.service.manager.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zttek.photosShareProject.common.myException.DBException;
import com.zttek.photosShareProject.modules.mapper.*;
import com.zttek.photosShareProject.modules.model.*;
import com.zttek.photosShareProject.modules.service.login.impl.UserServiceImpl;
import com.zttek.photosShareProject.modules.service.manager.IAbuseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by 13296 on 2017/8/17.
 */
@Service
public class AbuseServiceImpl implements IAbuseService {
    @Autowired
    private AbuseMapper abuseMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShareMapper shareMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private ExamineMapper examineMapper;
    @Autowired
    private InfoMapper infoMapper;

    /**
     * 获取举报列表
     * @return
     * @throws Exception
     */
    @Override
    public List<Abuse> selectAllAbuse() throws Exception {
        EntityWrapper<Abuse> ew = new EntityWrapper<>();
        ew.where("valid=1");
        List<Abuse> list = abuseMapper.selectList(ew);
        return list;
    }

    /**
     * 分页查询获取举报列表
     * @param page
     * @return
     * @throws Exception
     */
    @Override
    public Page<Abuse> selectAbusePage(Page<Abuse> page) throws Exception {
        EntityWrapper<Abuse> ew = new EntityWrapper<>();
        ew.where("valid=1");
        page.setRecords(abuseMapper.selectPage(page,ew));
        return page;
    }

    /**
     * 模糊查询举报
     * @param page
     * @param value
     * @param select
     * @return
     * @throws Exception
     */
    @Override
    public Page<Abuse> selectAbuseLikePage(Page<Abuse> page, String value,int select) throws Exception {
        //ew.setEntity(new Keyword());
        if (value.trim().length()==0&&value.trim().equals("")){
            page = selectAbusePage(page,select);
        }else{
            EntityWrapper<User> ew = new EntityWrapper<>();
            ew.where("valid=1").like("username",value).orderBy("createTime",false);
            List<User> uList = userMapper.selectList(ew);
        }

        return page;
    }

    @Override
    public Abuse selectAbuseById(Long id) throws Exception {
        Abuse abuse = abuseMapper.selectById(id);
        return abuse;
    }

    /**
     * 根据查看条件分页查询
     * @param page
     * @param select
     * @return
     * @throws Exception
     */
    @Override
    public Page<Abuse> selectAbusePage(Page<Abuse> page, int select) throws Exception {
        EntityWrapper<Abuse> ew = new EntityWrapper<>();

         /*select: -1-非审核，审核0-全部，1-已审核*/
        if(select==0){/*全部*/
            ew.where("valid=1").orderBy("createTime",false);
        }else if(select>0){/*已审核*/
            ew.where("valid=1").andNew("result<>0").orderBy("createTime",false);
        }else{/*非审核*/
            ew.where("valid=1").andNew("result=0").orderBy("createTime");
        }
        page.setRecords(abuseMapper.selectPage(page,ew));
        return page;
    }

    @Override
    public void deleteAbuseById(Long id) throws Exception {
        abuseMapper.deleteById(id);
    }


    @Override
    public void updateAbuse(Abuse abuse) throws Exception {
        abuseMapper.updateById(abuse);
    }


    @Override
    @Transactional
    public void dealExamine(Abuse abuse, Examine examine, Info infoToInformer, Info infoToInformant) throws Exception {
        abuseMapper.updateById(abuse);//保存审核结果
        if(abuse.getResult()==1){//删除被举报内容
            if(abuse.getType()==0){
                Share share = shareMapper.selectById(abuse.getTargetId());
                share.setValid(0);
                share.setDeleteTime(new Date());
                shareMapper.updateById(share);//逻辑删除分享
            }else {
                Comment comment = commentMapper.selectById(abuse.getTargetId());
                comment.setValid(0);
                comment.setDeleteTime(new Date());
                commentMapper.updateById(comment);//逻辑删除评论
            }
        }
        examineMapper.insert(examine);//保存审核结果
        infoMapper.insert(infoToInformer);//给举报者发通知
        if(abuse.getResult()==1){//通过给被举报者发通知
            infoMapper.insert(infoToInformant);
        }
    }


}
